#!/bin/bash

T=$1
shift

rm -f foo
mkfifo foo

#eval $1 <&0 >foo  &
$1 <&0 >foo 2>&1 &
j=$!
sleep 1 && tail -f foo --pid=$j &

while true ; do 
#    [ -e foo ] || break;
    touch foo1 -t $(date +%Y%m%d%H%M.%S -d "$T seconds ago")
    [ foo -ot foo1 ] && kill -15 $j >/dev/null 2>&1 && break
    sleep 1
done &
k=$!

wait $j
l=$?

sleep 2

#cat foo
if ! kill $k >/dev/null 2>&1 ; then

	echo $@ failed to terminate after $T seconds

else

	if [ $l -ge 128 ] ; then l=$(($l-128)); fi
	if [ $l -ne 0 ] ; then s="failed" ; else s="exited"; fi
	echo $@ $s with status $l $(kill -l $l)

fi

rm foo foo1

exit 0
